package com.zr.sms.servlet;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import net.sf.json.JSONObject;
/**
 * 打印订货订单功能
 * @author wangbs
 *
 */
public class DayindinghuoorderServlet extends HttpServlet {
	
      @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    		throws ServletException, IOException {
    	doPost(req, resp);
    }
      
      @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    		throws ServletException, IOException {
    	  req.setCharacterEncoding("utf-8");
    	  resp.setCharacterEncoding("utf-8");//重点
    	  
    	  String[] gids=req.getParameterValues("gid");
    	  StringBuffer gid=new StringBuffer("");
    	  for (int i = 0; i < gids.length-1; i++) {
			gid.append(gids[i]+",");
		}
    	  gid.append(gids[gids.length-1]);
    	  
    	  
    	  String[] gnames=req.getParameterValues("gname");
    	  StringBuffer gname=new StringBuffer("");
    	  for (int i = 0; i < gids.length-1; i++) {
    		  gname.append(gnames[i]+",");
		}
    	  gname.append(gnames[gids.length-1]);
    	  
    	  
    	  String[] gnumbers=req.getParameterValues("gnumber");
    	  StringBuffer gnumber=new StringBuffer("");
    	  for (int i = 0; i < gnumbers.length-1; i++) {
    		  gnumber.append(gnumbers[i]+",");
		}
    	  gnumber.append(gnumbers[gids.length-1]);
    	  
    	  String[] gcosts=req.getParameterValues("gcost");
    	  StringBuffer gcost=new StringBuffer("");
    	  for (int i = 0; i < gcosts.length-1; i++) {
    		  gcost.append(gcosts[i]+",");
		}
    	  gcost.append(gcosts[gids.length-1]);
    	  
    	  String[] snames=req.getParameterValues("sname");
    	  StringBuffer sname=new StringBuffer("");
    	  for (int i = 0; i < snames.length-1; i++) {
    		  sname.append(snames[i]+",");
		}
    	  sname.append(snames[gids.length-1]);
    	  
  
      String sumstr=gid.toString()+"#"+gname.toString()+"#"+gnumber.toString()+"#"+gcost.toString()+"#"+sname.toString();
   
   	  dachu(sumstr,resp,req);
      req.getRequestDispatcher("stockman/dhgl.jsp"); 
         
 		   

    }
      public void dachu(String data,HttpServletResponse resp,HttpServletRequest req) {
    	  resp.setContentType("application/octet-stream"); //重点
  		  resp.setCharacterEncoding("utf-8");//重点
  		  String name = "order.xls";
  		  String[] gids=req.getParameterValues("gid");
  		  try {
				name = URLEncoder.encode(name, "utf-8");//重点
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
  		  resp.addHeader("Content-Disposition", "attachment;filename=" + name);//重点
          
  		  
           String[] arr1 = data.split("#");
       
           String[][] arr3 = new String[gids.length+1][5];
   		    for (int i = 0; i < 5; i++) {
   		    String arr2[] = arr1[i].split(",");
   			for (int j = 0; j < gids.length; j++) {
   				
   				arr3[j][i] = arr2[j];
   			}
   		}
   		  
   		WritableWorkbook wwb;
   		try {
			//创建一个输出流
			OutputStream os = resp.getOutputStream();
			wwb=Workbook.createWorkbook(os);
			//创建一个工作页，第一个参数的页名，第二个参数表示该工作页在excel中处于哪一页
			WritableSheet sheet = wwb.createSheet("订货订单", 0);
			//设置所有列的列宽
			for (int i = 1; i<=7; i++) {
				sheet.setColumnView(i, 15);
			}
			//写入表头
			Label label1 = new Label(0,0,"商品编号");//填充第一行第一个单元格的内容
			sheet.addCell(label1);
			Label label2 = new Label(1,0,"商品名字");//填充第一行第一个单元格的内容
			sheet.addCell(label2);
			Label label3 = new Label(2,0,"订货数量");//填充第一行第一个单元格的内容
			sheet.addCell(label3);
			Label label4 = new Label(3,0,"商品进价");//填充第一行第一个单元格的内容
			sheet.addCell(label4);
			Label label5 = new Label(4,0,"供应商名");//填充第一行第一个单元格的内容
			sheet.addCell(label5);	
			//循环写入表内容
			for (int i = 0; i < gids.length; i++) {
				for (int j = 0; j < 5; j++) {
					Label label = new Label(j,i+1,arr3[i][j]);//
					sheet.addCell(label);
				}
			}
			wwb.write();//将内容写到excel文件中
			os.flush();//清空输出流
			wwb.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

      }

}
